package com.junyidao.game.service;

import com.alibaba.fastjson.JSONArray;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.junyidao.game.constant.BaseConstant;
import com.junyidao.game.constant.MsgDetailConstant;
import com.junyidao.game.core.LoginInfo;
import com.junyidao.game.mapper.*;
import com.junyidao.game.pojo.TMsgDetail;
import com.junyidao.game.pojo.TMsgSended;
import com.junyidao.game.vo.MsgSendedVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by zzh on 2016/8/24.
 */
@Service
@Transactional
public class MsgSendedService {

    @Autowired
    private TMsgSendedMapper msgSendedMapper;

    @Autowired
    private TMsgSendingMapper msgSendingMapper;

    @Autowired
    private SequenceService sequenceService;

    @Autowired
    private TMsgDetailMapper msgDetailMapper;

    @Autowired
    private TChannelMapper channelMapper;
    @Autowired
    private TGroupUserRelaMapper groupUserRelaMapper;

    public Map<String, Object> getSendedMsg(MsgSendedVo vo) {
        PageHelper.startPage(vo.getPage(), vo.getRows());
        List<MsgSendedVo> list = msgSendedMapper.getSendedMsg(vo);
        PageInfo pageInfo = new PageInfo(list);
        Map<String, Object> ret = new HashMap<>();
        ret.put("list", list);
        ret.put("count", pageInfo.getTotal());
        return ret;
    }

    public void delSended(String ids) {
        List<Integer> idList = JSONArray.parseArray(ids, Integer.class);
        Map<String, Object> map = new HashMap<>();
        map.put("ids", idList);
        msgSendedMapper.delSendedMsg(map);
    }

    public void addSendedMsg(MsgSendedVo vo, String ids, String uids, String channelIds, String groupIds) throws Exception {
        Integer id = sequenceService.getNextSequence("MSG_SENDED_ID");
        vo.setMsgSendedId(id);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        vo.setSendTime(sdf.parse(vo.getBeginTime()));
        vo.setCreateUid(LoginInfo.getUser().getUid());
        vo.setMsgStatus(BaseConstant.STATUS_NORMAL);
        msgSendedMapper.insert(vo);
        updateSendingMsg(ids, id);
        addUserMsg(uids, id);
        addChannelMsg(channelIds, id);
        addGroupMsg(groupIds, id);
    }

    public void updateSendingMsg(String ids, Integer msgSendedId) {
        if (!StringUtils.isEmpty(ids)) {
            List<Integer> idList = JSONArray.parseArray(ids, Integer.class);
            if (idList.size() > 0) {
                Map<String, Object> map = new HashMap<>();
                map.put("ids", idList);
                map.put("msgSendedId", msgSendedId);
                msgSendingMapper.updateMsgSended(map);
            }
        }
    }

    public void addUserMsg(String uids, Integer msgSendedId) {
        if (!StringUtils.isEmpty(uids)) {
            List<Integer> idList = JSONArray.parseArray(uids, Integer.class);
            if (idList.size() > 0) {
                for (Integer uid : idList) {
                    Integer id = sequenceService.getNextSequence("MSG_DETAIL_ID");
                    TMsgDetail detail = new TMsgDetail();
                    detail.setMsgDetailId(id);
                    detail.setMsgStatus(BaseConstant.STATUS_NORMAL);
                    detail.setReadFlag(MsgDetailConstant.NOT_READ);
                    detail.setMsgSendedId(msgSendedId);
                    detail.setMsgSource(MsgDetailConstant.MSG_SOURCE_USER);
                    detail.setObjectId(uid);
                    msgDetailMapper.insert(detail);
                }
            }
        }
    }

    public void addChannelMsg(String channelIds, Integer msgSendedId) {
        if (!StringUtils.isEmpty(channelIds)) {
            List<Integer> idList = JSONArray.parseArray(channelIds, Integer.class);
            if (idList.size() > 0) {
                Map<String, Object> map = new HashMap<>();
                map.put("ids", idList);
                List<Integer> uids = channelMapper.getUserIdsByChannelIds(map);
                for (Integer uid : uids) {
                    Integer id = sequenceService.getNextSequence("MSG_DETAIL_ID");
                    TMsgDetail detail = new TMsgDetail();
                    detail.setMsgDetailId(id);
                    detail.setMsgStatus(BaseConstant.STATUS_NORMAL);
                    detail.setReadFlag(MsgDetailConstant.NOT_READ);
                    detail.setMsgSendedId(msgSendedId);
                    detail.setMsgSource(MsgDetailConstant.MSG_SOURCE_CHANNEL);
                    detail.setObjectId(uid);
                    msgDetailMapper.insert(detail);
                }
            }
        }
    }

    public void addGroupMsg(String groupIds, Integer msgSendedId) {
        if (!StringUtils.isEmpty(groupIds)) {
            List<Integer> idList = JSONArray.parseArray(groupIds, Integer.class);
            if (idList.size() > 0) {
                Map<String, Object> map = new HashMap<>();
                map.put("ids", idList);
                List<Integer> uids = groupUserRelaMapper.getUidsByGroupIds(map);
                for (Integer uid : uids) {
                    Integer id = sequenceService.getNextSequence("MSG_DETAIL_ID");
                    TMsgDetail detail = new TMsgDetail();
                    detail.setMsgDetailId(id);
                    detail.setMsgStatus(BaseConstant.STATUS_NORMAL);
                    detail.setReadFlag(MsgDetailConstant.NOT_READ);
                    detail.setMsgSendedId(msgSendedId);
                    detail.setMsgSource(MsgDetailConstant.MSG_SOURCE_GROUP);
                    detail.setObjectId(uid);
                    msgDetailMapper.insert(detail);
                }
            }
        }
    }

    public MsgSendedVo getById(Integer msgId) {
        MsgSendedVo m = new MsgSendedVo();
        m.setMsgDetailId(msgId);
        MsgSendedVo msg = msgDetailMapper.getMsgDetail(m);
        return msg;
    }
}
